// 有多项任务, 以及每项任务的耗时, 要在n天做完, 并且每天可以看答案, 求每天最少用时
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let initArr = [];
let n;
rl.on("line", (line)=>{
    if(initArr.length === 0) {
        initArr = line.trim().split(/\s*,\s*/).map(Number)
    } else if(n === undefined) {
        n = Number(line.trim())
        console.log(getResult(initArr, n))

        // clear
        initArr.length = 0
        n = undefined
    }

})

function getResult(initArr, n) {

    //首先验证下, 每天看答案的情况
    if(initArr.length <= n) {
        return 0
    }


    // 然后每天正常做, 给一个时间值, 用于验证
    let min = 0
    let max = initArr.reduce((acc, num) => acc + num, 0)
    let ans = Infinity

    while(min <= max ){
        let mid = Math.floor((min + max)/2)
        if(valid(initArr, n, mid)) {
            ans = Math.min(mid, ans)
            max  = mid - 1
        } else {
            min = mid + 1
        }
    }
    return ans
}
function valid(initArr, n, mid) {
    // 验证单天是否足够
    let day = 0
    let index = 0
    let seeAnswer = false
    let max = 0
    let total = 0

    while(index < initArr.length) {
        let num = initArr[index]
        max = Math.max(max, num)
        total += num
        if(total < mid) {
            index++
        } else if(total > num && seeAnswer === false) { // 虽然超限, 但还没看答案
            total -= max
            seeAnswer = true
            index++
        } else if (total > num && seeAnswer === true) { // 超限, 且已看答案
            day++
            seeAnswer = false
            max = 0
            total = 0
        }
    }
    return day <= n
}